package com.taichuan.selfcheck.ui.fragment;

import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import com.taichuan.keysupport.Key;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.mvp.presenter.MicrophonePresenter;
import com.taichuan.selfcheck.mvp.viewinterface.MicrophoneInterface;
import com.taichuan.selfcheck.ui.dialog.ResultDialog;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2019/12/4
 */
public class MicrophoneFragment extends AppBaseFragment<MicrophoneInterface, MicrophonePresenter> implements MicrophoneInterface {


    @BindView(R.id.btn_record)
    Button btnRecord;
    @BindView(R.id.btn_play)
    Button btnPlay;
    @BindView(R.id.sb_setSys_media)
    SeekBar mSeekMedia; // 媒体

    ResultDialog resultDialog;
    private int  curMediaProgress;
    @Override
    protected void getBundleValues(Bundle bundle) {

    }

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_microphone;
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        resultDialog = new ResultDialog(PropertyDefaultValue.FUNCTION_TEST_3,getResources().getString(R.string.microphone),getContext());            //设置弹窗发送的事件——麦克风测试
        mPresenter.initSeekBar();
    }

    private SeekBar.OnSeekBarChangeListener mSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            mPresenter.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            mPresenter.playVolume();
        }

    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.StopAll();
    }

    @OnClick({R.id.btn_record,R.id.btn_play})
    @Override
    protected void onXmlClick(View v) {
        super.onXmlClick(v);
        switch (v.getId()) {
            case R.id.btn_record: {
                mPresenter.stopRing();
                mPresenter.RecordSound(btnRecord);
                break;
            }
            case R.id.btn_play: {
                mPresenter.stopRing();
                mPresenter.PlaySound(getContext());
                break;
            }
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        mPresenter.stopRing();
        if (resultDialog.isShowing()){
            resultDialog.dispatchKeyEvent(event);
        }
        if (event.getKeyCode() == Key.Convert.KEYCODE_1) {
           //降低音量
            if (curMediaProgress > 0) {
                curMediaProgress--;
                mSeekMedia.setProgress(curMediaProgress);
                mPresenter.setStreamVolume(AudioManager.STREAM_MUSIC, curMediaProgress, 0);
                mPresenter.playVolume();
            }
        }else if (event.getKeyCode() == Key.Convert.KEYCODE_3) {
            //提高音量
            if (curMediaProgress < 15) {
                curMediaProgress++;
                mSeekMedia.setProgress(curMediaProgress);
                mPresenter.setStreamVolume(AudioManager.STREAM_MUSIC, curMediaProgress, 0);
                mPresenter.playVolume();
            }
        }else if (event.getKeyCode() == Key.Convert.KEYCODE_2) {
            btnRecord.performClick();
        }else if (event.getKeyCode() == Key.Convert.KEYCODE_8){
            btnPlay.performClick();
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected MicrophonePresenter createPresenter() {
        return new MicrophonePresenter(getActivity());
    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {
        resultDialog.show();
    }

    @Override
    public void playSound(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            showMsg("录音保存成功！");
            Log.i(MicrophoneFragment.class.getSimpleName(), "playSound: 存储成功");
        }
    }

    @Override
    public void initSeekBar(int currentVolume) {
        curMediaProgress=currentVolume;
        mSeekMedia.setProgress(currentVolume);
        mSeekMedia.setMax(15);
        mSeekMedia.setOnSeekBarChangeListener(mSeekBarChangeListener);
    }
}
